#include<iostream>

using namespace std;

#define MAXSIZE 100    //最大长度
#define  OVERFLOW -2
#define  OK 1
#define  ERROR 0

typedef int ElemType;
typedef int Status;
typedef  struct {
    ElemType* elem;      //指向数据元素的基地址
    int  length;          //线性表的当前长度                                                      
}SqList;

Status InitList_Sq(SqList& L) {    //构造一个空的顺序表L
    if (MAXSIZE > 1000000) return ERROR;
    L.elem = new ElemType[MAXSIZE];   //为顺序表分配空间
    if (!L.elem) exit(OVERFLOW);       //存储分配失败
    L.length = 0;   //空表长度为0
    return OK;
}

Status GetElem(SqList L, int i, ElemType& e)
{
    if (i<1 || i>L.length) return ERROR;
    //判断i值是否合理，若不合理，返回ERROR
    e = L.elem[i - 1];   //第i-1的单元存储着第i个数据
    return OK;
}

Status ListInsert_Sq(SqList& L, int i, ElemType e) {   //插入功能
    if ((i < 1) || (i > L.length + 1)) return ERROR;
    if (L.length == MAXSIZE) return ERROR;
    for (int j = L.length - 1; j >= i - 1; j--)  
        L.elem[j + 1] = L.elem[j]; 
    L.elem[i - 1] = e;
    ++L.length;
    return OK;
}

Status ListDelet_Sq(SqList& L, int i) { //删除功能
    if ((i < 1) || (i > L.length)) return ERROR; 
    for (int j = i; j < L.length; j++) 
        L.elem[j - 1] = L.elem[j];
    --L.length;
    return OK;
}

int main()
{
    SqList L;
    int i;
    ElemType e;

    //初始化顺序表
    if (InitList_Sq(L)) cout << "成功初始化顺序表！" << endl;
    else cout << "失败！";

    L.length = 10;
    for (i = 0; i < L.length; i++) L.elem[i] = i + 1;

    //查询列表
    cout << "请输入要查询的位序i：";
    cin >> i;
    if (GetElem(L, i, e)) cout << endl << "这个元素是：" << e << endl;
    else cout << "没有这个元素！" << endl;

    cout << "当前顺序表的元素有：";
    for (i = 0; i < L.length; i++)cout << L.elem[i] << " ";
    cout<<endl;

    //插入列表元素
    cout << "请输入要插入的元素的位序i和值e：";
    cin >> i >> e;
    if (ListInsert_Sq(L, i, e)) cout << endl << "这个元素是的位序i是：" << i << "，这个元素的值e是：" << e << endl;
    else cout << "插入失败！" << endl;
    
    cout << "当前顺序表的元素有：";
    for (i = 0; i < L.length; i++)cout << L.elem[i] << " ";
    cout<<endl;

    //删除列表元素
    cout << "请输入需要删除的元素的位序i：";
    cin >> i ;
    if (ListDelet_Sq(L, i))
        cout << "元素成功删除" << endl;
    else
        cout << "删除失败！" << endl;

    cout << "当前顺序表的元素有：";
    for (i = 0; i < L.length; i++)cout << L.elem[i] << " ";
    
    return 0;  // 添加return语句
}